// 分隔符列表节点，是一个双向链表

import { MNode } from "./mnodes";

export default class DelimiterNode {
  public char: string = ''; // 符号字符
  public count: number = 0; // 字符个数
  public currentCount: number = 0;  // 当前剩余字符个数
  public mnode: MNode;  // 符号指向的对应节点树中的节点
  public pre: DelimiterNode | null = null;  // 前符号节点
  public next: DelimiterNode | null = null; // 后符号节点
  
  constructor (char: string, count: number, mnode: MNode) {
    this.char = char;
    this.count = count;
    this.currentCount = count;
    this.mnode = mnode;
    this.pre = null;
    this.next = null;
  }

  // 删除链表某个区间中的节点，删除区间为 (left, right)
  static removeBetween (left: DelimiterNode, right: DelimiterNode) {
    if(left.next !== right) {
      left.next = right;
      right.pre = left;
    }
  }

  // 从链表中删除当前节点
  remove() {
    if(this.pre != null) {
      this.pre.next = this.next;
    }
    if (this.next != null) {
      this.next.pre = this.pre;
    }
  }
}